'##############
'Heizungs-Script
'##############

'Simuliert Wagenheizung, Zusatzheizung (evtl. auch gleich den Kühlkreislauf mit...)

'(c) 20.02.2009 Rüdiger Hülsmann

'Script Version: 1.1
'Omsi release: 1.0

'Needs:
'cockpit (für Taster und Regler)
'engine (for cooling circuit)
'door (for door opening status)

'Revision History:
'- Rüdiger Hülsmann	17.02.2009	File created
'- Rüdiger Hülsmann	20.02.2009	Batterietrennschalter eingefügt (v1.1)
'- Marcel Kuhnt		10.08.2009	Added Revision History
'- Marcel & Rüdiger	23.09.2010	Parking Heater, room temperature
'- RÜdiger Hülsmann	16.11.2010	Front heater
'- Rüdiger Hülsmann	18.11.2010	Improved front heater
'- Rüdiger Hülsmann	20.11.2010	Cabin heater, values adjusted
'- Rüdiger Hülsmann	22.11.2010	Auxheat Schmitt-triggered, engine coolable by fan heaters
'- Rüdiger Hülsmann	10.01.2011	Driver window airflow
'- Morozov Maxim 	06.11.2011  Script adapted for Heating Control Panel system (MAN NL202)

'------------------------------------------------------------------------------------------


{trigger:HCP_aux_heater_sw}
	(L.L.elec_busbar_main)
	{if}
		(L.L.cp_HCP_aux_heater_target) ! (S.L.cp_HCP_aux_heater_target)
	{endif}
{end}

'---------------------------------------

{trigger:HCP_vent_100}
		(L.L.HCP_vent_select) 0 > 
		(L.L.HCP_vent_select) 3 = ! ||
			{if}
				3 (S.L.HCP_vent_select)
				0 (S.L.HCP_vent_77) (S.L.HCP_vent_50) (S.L.HCP_vent_0)
			{endif}
{end}

{trigger:HCP_vent_77}
		(L.L.HCP_vent_select) 0 > 
		(L.L.HCP_vent_select) 2 = ! ||
			{if}
				2 (S.L.HCP_vent_select)
				0 (S.L.HCP_vent_100) (S.L.HCP_vent_50) (S.L.HCP_vent_0)
			{endif}
{end}

{trigger:HCP_vent_50}
		(L.L.HCP_vent_select) 0 > 
		(L.L.HCP_vent_select) 1 = ! ||
			{if}
				1 (S.L.HCP_vent_select)
				0 (S.L.HCP_vent_100) (S.L.HCP_vent_77) (S.L.HCP_vent_0)
		{endif}
{end}

{trigger:HCP_vent_0}
		(L.L.HCP_vent_select) 0 < 
		(L.L.HCP_vent_select) 0 = ! ||
			{if}
				0 (S.L.HCP_vent_select)
				0 (S.L.HCP_vent_100) (S.L.HCP_vent_77) (S.L.HCP_vent_50)
			{endif}
{end}

'---------------------------------------

{trigger:HCP_air_out}
		(L.L.HCP_air_select) 2 = !
			{if}
				2 (S.L.HCP_air_select)
				0 (S.L.HCP_air_mix) (S.L.HCP_air_int)
			{endif}
{end}

{trigger:HCP_air_mix}
		(L.L.HCP_air_select) 1 = !
			{if}
				1 (S.L.HCP_air_select)
				0 (S.L.HCP_air_out) (S.L.HCP_air_int)
			{endif}
{end}

{trigger:HCP_air_int}
		(L.L.HCP_air_select) 0 = !
			{if}
				0 (S.L.HCP_air_select)
				0 (S.L.HCP_air_out) (S.L.HCP_air_mix)
			{endif}
{end}

'----------------------
'	Init
'----------------------
{macro:heizung_init}
	(L.S.Weather_Temperature) (S.L.Cabinair_Temp)
	(L.S.Weather_AbsHum) (S.L.Cabinair_absHum)
	0 (S.L.HCP_vent_select)
	0 (S.L.HCP_air_select)
{end}

{macro:heizung_frame}

	(M.L.standheizung_frame)	
	(M.L.cabinair_frame)
	(M.L.HCP_vent_frame)
	(M.L.HCP_temp_indic_frame)
	(M.L.HCP_buttons_lights)
{end}

{macro:HCP_vent_frame}

	(L.L.HCP_vent_select) 1 =	
	{if}
		(C.L.HCP_vent_low_RPM) (S.L.cp_klimator_RPM)
		1 s1
	{else}
		0 s1
	{endif}

	l1 100 * (L.L.HCP_vent_low_RPM) > 
	{if}
		6 s0
	{else}
		1.0 s0
	{endif}

	l1 100 * (L.L.HCP_vent_low_RPM) - s0 * (L.S.Timegap) * (L.L.HCP_vent_low_RPM) + (S.L.HCP_vent_low_RPM)
	
	(L.L.HCP_vent_select) 2 =
	{if}
		(C.L.HCP_vent_mid_RPM) (S.L.cp_klimator_RPM)
		1 s1
	{else}
		0 s1
	{endif}

	l1 100 * (L.L.HCP_vent_mid_RPM) > 
	{if}
		6 s0
	{else}
		1.0 s0
	{endif}

	l1 100 * (L.L.HCP_vent_mid_RPM) - s0 * (L.S.Timegap) * (L.L.HCP_vent_mid_RPM) + (S.L.HCP_vent_mid_RPM)
	
	(L.L.HCP_vent_select) 3 =
	{if}
		(C.L.HCP_vent_hi_RPM) (S.L.cp_klimator_RPM)
		1 s1
	{else}
		0 s1
	{endif}

	l1 100 * (L.L.HCP_vent_hi_RPM) > 
	{if}
		6 s0
	{else}
		1.0 s0
	{endif}

	l1 100 * (L.L.HCP_vent_hi_RPM) - s0 * (L.S.Timegap) * (L.L.HCP_vent_hi_RPM) + (S.L.HCP_vent_hi_RPM)
	
	(L.L.HCP_vent_select) 0 =
	{if}
	0 (S.L.cp_klimator_RPM)
	{endif}
	
{end}

{macro:HCP_buttons_lights}

	(L.L.elec_busbar_main)
	{if}
		(L.L.HCP_vent_select) 3 =
		{if}
			1 (S.L.HCP_vent_100)
		{endif}
	{else}
		(L.L.HCP_vent_select) 3 =
		{if}
			0 (S.L.HCP_vent_100)
		{endif}
	{endif}
	
	(L.L.elec_busbar_main)
	{if}
		(L.L.HCP_vent_select) 2 =
		{if}
			1 (S.L.HCP_vent_77)
		{endif}
	{else}
		(L.L.HCP_vent_select) 2 =
		{if}
			0 (S.L.HCP_vent_77)
		{endif}
	{endif}		


	(L.L.elec_busbar_main)
	{if}
		(L.L.HCP_vent_select) 1 =
		{if}
			1 (S.L.HCP_vent_50)
		{endif}
	{else}
		(L.L.HCP_vent_select) 1 =
		{if}
			0 (S.L.HCP_vent_50)
		{endif}
	{endif}
	
	(L.L.elec_busbar_main)
	{if}
		(L.L.HCP_vent_select) 0 =
		{if}
			1 (S.L.HCP_vent_0)
		{endif}
	{else}
		(L.L.HCP_vent_select) 0 =
		{if}
			0 (S.L.HCP_vent_0)
		{endif}
	{endif}
	
	(L.L.elec_busbar_main)
	{if}
		(L.L.HCP_air_select) 2 =
		{if}
			1 (S.L.HCP_air_out)
		{endif}
	{else}
		(L.L.HCP_air_select) 2 =
		{if}
			0 (S.L.HCP_air_out)
		{endif}
	{endif}
	
	(L.L.elec_busbar_main)
	{if}
		(L.L.HCP_air_select) 1 =
		{if}
			1 (S.L.HCP_air_mix)
		{endif}
	{else}
		(L.L.HCP_air_select) 1 =
		{if}
			0 (S.L.HCP_air_mix)
		{endif}
	{endif}

	(L.L.elec_busbar_main)
	{if}
		(L.L.HCP_air_select) 0 =
		{if}
			1 (S.L.HCP_air_int)
		{endif}
	{else}
		(L.L.HCP_air_select) 0 =
		{if}
			0 (S.L.HCP_air_int)
		{endif}
	{endif}
{end}
	
{macro:HCP_temp_indic_frame}

	(L.L.elec_busbar_main)
	(L.L.cockpit_heizregler_temp) 0.1 >= &&
	{if}
		1 (S.L.HCP_temp_indic_1)
	{else}
		0 (S.L.HCP_temp_indic_1)
	{endif}
	
	(L.L.elec_busbar_main)
	(L.L.cockpit_heizregler_temp) 0.2 >= &&
	{if}
		1 (S.L.HCP_temp_indic_2)
	{else}
		0 (S.L.HCP_temp_indic_2)
	{endif}
	
	(L.L.elec_busbar_main)
	(L.L.cockpit_heizregler_temp) 0.3 >= &&
	{if}
		1 (S.L.HCP_temp_indic_3)
	{else}
		0 (S.L.HCP_temp_indic_3)
	{endif}
	
	(L.L.elec_busbar_main)
	(L.L.cockpit_heizregler_temp) 0.4 >= &&
	{if}
		1 (S.L.HCP_temp_indic_4)
	{else}
		0 (S.L.HCP_temp_indic_4)
	{endif}
	
	(L.L.elec_busbar_main)
	(L.L.cockpit_heizregler_temp) 0.5 >= &&
	{if}
		1 (S.L.HCP_temp_indic_5)
	{else}
		0 (S.L.HCP_temp_indic_5)
	{endif}
	
	(L.L.elec_busbar_main)
	(L.L.cockpit_heizregler_temp) 0.6 >= &&
	{if}
		1 (S.L.HCP_temp_indic_6)
	{else}
		0 (S.L.HCP_temp_indic_6)
	{endif}
	
	(L.L.elec_busbar_main)
	(L.L.cockpit_heizregler_temp) 0.7 >= &&
	{if}
		1 (S.L.HCP_temp_indic_7)
	{else}
		0 (S.L.HCP_temp_indic_7)
	{endif}
	
	(L.L.elec_busbar_main)
	(L.L.cockpit_heizregler_temp) 0.8 >= &&
	{if}
		1 (S.L.HCP_temp_indic_8)
	{else}
		0 (S.L.HCP_temp_indic_8)
	{endif}
	
	(L.L.elec_busbar_main)
	(L.L.cockpit_heizregler_temp) 0.9 >= &&
	{if}
		1 (S.L.HCP_temp_indic_9)
	{else}
		0 (S.L.HCP_temp_indic_9)
	{endif}
{end}




{macro:cabinair_frame}

'	Berechnung der Raten:
	(L.L.Cabinair_Temp) (L.S.Weather_Temperature) - (C.L.cabinair_Qrate_envir_factor) * (S.L.cabinair_Qrate_envir)

'	Direkte Ableitung der Motorwärme in den Fahrgastraum
	(L.L.engine_temperature_envir) (L.L.Cabinair_Temp) - (C.L.cabinair_Qrate_engine_factor) * (S.L.cabinair_Qrate_engine)


'	Berechnung der Solltemperatur am Frontheizgerät
	(L.L.HCP_air_select) 0 =
	{if}
		(L.L.Cabinair_Temp) (L.L.engine_temperature) (L.L.Cabinair_Temp) - (L.L.cockpit_heizregler_temp) * (C.L.heat_exchanger_effectivity) * + (S.L.fanheater_t_target)
	{else}
		l0 2 =
		{if}
'			Frischluftheizung:
			(L.S.Weather_Temperature) (L.L.engine_temperature) (L.S.Weather_Temperature) - (L.L.cockpit_heizregler_temp) * (C.L.heat_exchanger_effectivity) * + (S.L.fanheater_t_target)
		{else}
'			Direktbelüftung:
			(L.S.Weather_Temperature) (S.L.fanheater_t_target)
		{endif}		
	{endif}


'Berechnung der effektiven Gesamt-Luftaustauschfläche:
	(L.L.passenger_door_pos) (L.L.driver_door_pos) (L.L.emg_door_L_pos) + + (C.L.cabinair_A_perdoor) *

	(L.L.window_1_pos) (L.L.window_2_pos) (L.L.window_3_pos) (L.L.window_4_pos) + + +
	(L.L.window_5_pos) (L.L.window_6_pos) (L.L.window_7_pos) (L.L.window_8_pos) + + + +
	(C.L.cabinair_A_perwindow) * + 
	(L.L.cp_fahrerfenster_pos) (C.L.cabinair_A_driverwindow) * +

'Berechnung des Gesamtvolumenstroms in die Außenwelt und umgekehrt (V zählt je Richtung, d.h. insgesamt wird 2*V bewegt)
	(L.L.velocity) 20 / abs (C.L.cabinair_windowdoor_minkmh) + (C.L.cabinair_windowdoor_effectivity) * 3.6 / * (S.L.cabinair_Vrate_doorwindow)

'Berechnung des Volumenstroms durchs Frontheizgerät
	(L.L.HCP_air_select) 1 =
	{if}
		(C.L.cabinair_A_frontheater) (L.L.velocity) 20 / abs (C.L.cabinair_windowdoor_minkmh) + (C.L.cabinair_windowdoor_effectivity) * 3.6 / * (S.L.cabinair_Vrate_fanheater)
	{else}
		(C.L.cabinair_A_frontheater) (L.L.cp_klimator_RPM) 100 / * (S.L.cabinair_Vrate_fanheater)
	{endif}

'Berechnung des Volumenstroms durch die Raumheizgeräte
	(C.L.cabinair_A_cabinheater) (L.L.cp_heizluefter_sw) 1 = * (L.L.elec_busbar_main) 1 = * (S.L.cabinair_Vrate_cabinheater)



'Berechnung der Temperaturrate durch Motor und Abstrahlung an die Umwelt...
	(L.L.cabinair_Qrate_engine) (L.L.cabinair_Qrate_envir) - (C.L.cabinair_cvm) /
'...und Temperaturrate durch Passagiere...
	(C.L.cabinair_T_passenger) (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (C.L.cabinair_Vrate_passenger) * (L.L.humans_count) * +
'...und dieser durch die Volumenströme durch Fenster und Türen:
	(L.S.Weather_Temperature) (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_doorwindow) * +
'...und des Frontheizgerätes...
	(L.L.fanheater_t_target) (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_fanheater) * +
'...und der Raumheizgeräte...
	(L.L.engine_temperature) (C.L.heat_exchanger_effectivity) * (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_cabinheater) * +
'...aufintegrieren:
	(L.S.Timegap) * (L.L.Cabinair_Temp) + (S.L.Cabinair_Temp)

'Berechnung der Motorkühlrate durch die Wärmetauscher in den Heizgeräten:

	(L.L.fanheater_t_target) (L.L.Cabinair_Temp) - 0 max (C.L.cabinair_V) / (L.L.cabinair_Vrate_fanheater) * 
	(L.L.engine_temperature) (C.L.heat_exchanger_effectivity) * (L.L.Cabinair_Temp) max (L.L.Cabinair_Temp) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_cabinheater) * +
	(C.L.cabinair_cvm) * 8 * (S.L.cabinair_Qrate_engine_fanheatcooling)



'Berechnung der Feuchtigkeitsrate durch Volströme mit den Passagieren:
	(C.L.cabinair_absHum_passenger) (L.L.Cabinair_absHum) - (C.L.cabinair_V) / (C.L.cabinair_Vrate_passenger) * (L.L.humans_count) *
'Berechnung der Feuchtigkeitsrate durch Volumenströme mit der Außenwelt:
	(L.S.Weather_AbsHum) (L.L.Cabinair_absHum) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_doorwindow) * +
'Berechnung der Feuchtigkeitsrate durch Frontheizgerät
	(L.S.Weather_AbsHum) (L.L.Cabinair_absHum) - (C.L.cabinair_V) / (L.L.cabinair_Vrate_fanheater) * (L.L.HCP_air_select) 1 >= * + (S.L.cabinair_test)
'... aufintegrieren:
	(L.S.Timegap) * (L.L.Cabinair_absHum) + (S.L.Cabinair_absHum)



{end}






{macro:standheizung_frame}


	(L.L.cp_HCP_aux_heater_target) (L.L.elec_busbar_avail) &&
	{if}
		(L.L.standheizung_running) !
		{if}
			0 (S.L.standheizung_volume_stufe1) (S.L.standheizung_volume_stufe2)
			1 (S.L.standheizung_running)  
			0 (S.L.standheizung_stufe1_time_elapsed)
			1 (S.L.standheizung_stufe1) 
		{endif}
		1 (S.L.standheizung_target)
	
	{else}
		0 (S.L.standheizung_target)
	{endif}
	



'	Raufrödeln der Timer:
	(L.L.standheizung_running)
	{if}
		(L.L.standheizung_stufe1_time_elapsed) (L.S.Timegap) + (S.L.standheizung_stufe1_time_elapsed)
		(L.L.standheizung_stufe2_time_elapsed) (L.S.Timegap) + (S.L.standheizung_stufe2_time_elapsed)
	{endif}

'	Einschalten der 2. Stufe:
	(L.L.standheizung_target) (L.L.standheizung_stufe1_time_elapsed) (C.L.standheizung_vorlauf) > &&
	(L.L.engine_temperature) (C.L.standheizung_brenner_t_ein) < &&
	{if}
		(L.L.standheizung_stufe2) !
		{if}
			0 (S.L.standheizung_stufe2_time_elapsed)
		{endif}
		1 (S.L.standheizung_stufe2)
	{endif}

'	Ausschalten der 2. Stufe:
	(L.L.standheizung_target) ! (L.L.standheizung_stufe2_time_elapsed) (C.L.standheizung_minlauf) > &&
	(L.L.engine_temperature) (C.L.standheizung_brenner_t_aus) > ||
	{if}
		(L.L.standheizung_stufe2)
		{if}
			0 (S.L.standheizung_stufe1_time_elapsed)
		{endif}
		0 (S.L.standheizung_stufe2) 
	{endif}

'	Ausschalten der 1. Stufe:
	(L.L.standheizung_target) ! (L.L.standheizung_stufe1_time_elapsed) (C.L.standheizung_nachlauf) > && 
	{if}
		0 (S.L.standheizung_stufe1)
	{endif}

	(L.L.standheizung_stufe1) 
	{if}
		(L.L.standheizung_volume_stufe1) (L.S.Timegap) 3 / + 1 min (S.L.standheizung_volume_stufe1)
	{else}
		(L.L.standheizung_volume_stufe1) (L.S.Timegap) 3 / - 0 max (S.L.standheizung_volume_stufe1)
		0.01 <
		{if}
			0 (S.L.standheizung_running)
		{endif}
	{endif}

	(L.L.standheizung_stufe2) 
	{if}
		(L.L.standheizung_volume_stufe2) (L.S.Timegap) 4 / + 1 min (S.L.standheizung_volume_stufe2)

'		Berechnung der Temperaturveränderung:
		(C.L.heizung_Qrate_standheizung) (L.L.cabinair_Qrate_envir) - (C.L.engine_cvm_engine) / (L.S.Timegap) * (L.L.engine_temperature) + (S.L.engine_temperature) 


	{else}
		(L.L.standheizung_volume_stufe2) (L.S.Timegap) 4 / - 0 max (S.L.standheizung_volume_stufe2)
	{endif}



{end}
